<template>
	<view class="mask" @tap="cancel" @touchmove.stop>
		<view class="mask-content  animated bounceInUp" @tap.stop>
			<text class="new-iconfont icon-close close-btn" @tap="cancel"></text>
			<view class="title">
				{{info.title}}
			</view>
			<view class="block">
				<view class="item-title">可用优惠券:</view>
				<view class="arrow-right bold" @tap="isCouponPopup = true">
					<text class="meta" v-if="order.coupon_discount"> -
						¥{{$tool.formatPrice(order.coupon_discount)}}</text>
					<template v-else>
						<text class="meta" style="color: #FF1A1A;"
							v-if="usableCoupons.length">{{usableCoupons.length}}张可用</text>
						<text class="meta gray-text" v-else>暂无可用优惠券</text>
					</template>
					<text class="new-iconfont icon-arrow-right"></text>
				</view>
			</view>
			<view class="block">
				<view class="item-title">可用红包:</view>
				<view class="arrow-right bold">
					<template v-if="order.redpack">
						<text class="meta"> ¥{{$tool.formatPrice(order.redpack)}}</text>
						<switch class="redpack-switch" :checked="order.is_use_redpack" @change="switchChange" />
					</template>
					<text class="meta" v-else> ¥0 </text>
				</view>
			</view><!--block end-->

			<view class="block" v-if="order.max_useable_score">
				<view class="item-title">
					{{scoreAlias}}抵扣
				</view>
				<view class="arrow-right bold">
					<text class="meta"> {{order.max_useable_score}}{{scoreAlias}}</text>
					<text class="meta red" style="margin-left: 10rpx;" v-if="order.score_discount">
						-¥{{order.score_discount | priceToFixed}}</text>
					<switch v-if="order.max_useable_score" class="redpack-switch" :checked="order.is_use_score"
						@change="scoreSwitchChange" />
				</view>
			</view>
			<view class="total">
				小计：
				<PriceDisplay :info="order" prefix="pay_"></PriceDisplay>
			</view>

			<!-- 用户协议 -->
			<UserStatement v-model="isCheckUserStatement"></UserStatement>

			<view class="button" @tap="disableMultiClick(submit)" v-if="isInit && !isSubmiting">
				<text>确认购买</text>
			</view>
			<view class="button disabled" v-else>
				<text>确认购买</text>
			</view>
		</view>

		<UsableCouponPopup v-if="isCouponPopup" :unusableCoupons="unusableCoupons" :usableCoupons="usableCoupons"
			@change="couponChange" @close="isCouponPopup = false">
		</UsableCouponPopup>

	</view>
</template>

<script>
	import payment from "@/utils/payment.js"
	export default {
		components: {},
		data() {
			return {
				payTotal: 1,//支付总计
				order: {},//订单
				price: 0,//价格
				form: {
					is_use_redpack: 0,
					is_use_score: 0
				},
				currentCoupon: {},//当前优惠券
				isCouponPopup: false,
				unusableCoupons: [],
				usableCoupons: [],
				isInit: false,
				isCheckUserStatement: true,
				isSubmiting: false
			}
		},
		props: {
			info: {
				type: Object
			}
		},
		computed: {

		},
		watch: {
			payTotal() {
				// this.initOrderEvent()
			}
		},
		created() {
			this.payTotal = this.info.payNum;
			this.initOrderEvent()
		},
		methods: {
			formatPrice(price) {
				return (price / 100).toFixed(2)
			},
			couponChange(e) {
				if (e.id === this.currentCoupon.id) {
					// 1、再次点击取消使用优惠券  
					// 2、暂时关闭此功能
					// this.currentCoupon = {}
				} else {
					this.currentCoupon = e
					this.initOrderEvent()
				}
			},
			//初始化订单
			initOrderEvent() {
				console.log(this.info, 1222);
				uni.showLoading()
				this.$http('/choushang/order/preview', 'POST', {
					activity_id: this.info.activity_id,
					total: this.payTotal,
					coupon_id: this.currentCoupon.id,
					...this.form
				}).then(res => {
					this.isInit = true
					this.order = res.data.order,
						this.unusableCoupons = res.data.order.coupons.unusable
					this.usableCoupons = res.data.order.coupons.usable
					uni.hideLoading()
				}).catch(err => {
					this.isInit = false
					this.cancel()
				})
			},
			//更换信息
			switchChange(e) {
				this.form.is_use_redpack = e.detail.value ? 1 : 0
				this.initOrderEvent()
			},
			//统计更新
			scoreSwitchChange(e) {
				this.form.is_use_score = e.detail.value ? 1 : 0
				this.initOrderEvent()
			},
			//取消
			cancel() {
				this.$emit('close')
			},
			//关闭支付弹窗
			childMethod() {
				this.$parent.hidePayPopup();
			},
			//创建订单
			createOrder() {
				if (this.isSubmiting) {
					return false
				}

				uni.showLoading({
					title: '提交中',
					icon: 'none'
				})

				this.isSubmiting = true
				setTimeout(() => {
					this.isSubmiting = false
				}, 1500)
				//提交请求
				this.$http('/choushangs/order/confirm', 'POST', {
					sharer:uni.getStorageSync('inviter'),
					activity_id: this.info.activity_id,
					total: this.payTotal,
					coupon_id: this.currentCoupon.id,
					...this.form
				}).then(res => {
					uni.hideLoading()
					console.log(res.code,331111);
					let info = res.data
					if (res.code == 222) {
						var that = this;
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 2000,
							success() {
								that.childMethod()
							}
						})
					}else {
						if (info.is_need_pay) {
							payment.pay({
								pay_config: info.pay_config,
								success: () => {
									// uni.showToast({
									// 	title: "支付成功",
									// 	icon: "none"
									// })
									// 支付成功
									console.log(info, 8311);
									this.$emit('success', info.order)
								},
								fail: () => {
									uni.showToast({
										title: "支付失败",
										icon: "none"
									})

									// 支付失败，关闭订单
									this.$http(`/orders/${info.order.uuid}`, 'PUT', {
										type: 'close_and_delete'
									})
								}
							})
						} else {
							console.log(info, 1111);
							this.$emit('success', info.order)
						}
					}
				})
			},
			//提交数据
			submit() {
				if (!this.isCheckUserStatement) {
					uni.showToast({
						title: '请先勾选《用户使用协议》',
						icon: 'none'
					})
					return false
				}

				// 微信小程序请求订阅消息
				// #ifdef MP-WEIXIN
				uni.requestSubscribeMessage({
					tmplIds: [
						this.miniappSubscribeIds.order_delivered, // 发货提醒
						this.miniappSubscribeIds.order_paid, // 支付成功通知
					],
					complete: (res) => {
						this.createOrder()
					}
				})
				
				// if (this.miniappSubscribeIds) {
				// 	uni.requestSubscribeMessage({
				// 		tmplIds: [
				// 			this.miniappSubscribeIds.order_delivered, // 发货提醒
				// 			this.miniappSubscribeIds.order_paid, // 支付成功通知
				// 		],
				// 		complete: (res) => {
				// 			this.createOrder()
				// 		}
				// 	})
				// } else {
				// 	this.createOrder()
				// }
				// #endif
				// #ifndef MP-WEIXIN
				this.createOrder()
				// #endif
			}
		},
		onPageScroll(e) {}
	}
</script>

<style lang="scss" scoped>
	.small-text {
		// font-size: 80%;
	}

	.mask-content {
		position: absolute;
		bottom: 0;
		width: 100%;
		// height: 680rpx;
		background-color: #EDEDED;
		background-color: white;
		border-radius: 10rpx 10rpx 0 0;
		// display: flex;
		// flex-direction: column;
		// align-items: center;
		// animation: show 0.8s;
		padding: 50rpx 40rpx;
		box-sizing: border-box;

		.close-btn {
			position: absolute;
			right: 20rpx;
			top: 20rpx;
		}

		.title {
			text-align: center;
			font-size: 36rpx;
			font-weight: 500;
		}

		.block {
			display: flex;
			align-items: center;
			padding: 16rpx 0rpx;

			.item-title {
				// margin: 0rpx 0rpx 0rpx 0rpx;
				font-size: 28rpx;
				// color: gray;
				flex-grow: 1;
				font-weight: 500;
			}

			// .total-list {
			// 	display: flex;
			// 	.item {
			// 		font-weight: 500;
			// 		border-radius: 40rpx;
			// 		border: 2rpx solid #e1e1e1;
			// 		// background: #e1e1e1;
			// 		padding: 4rpx 20rpx;
			// 		margin-left: 20rpx;
			// 		font-size: 24rpx;
			// 		position: relative;

			// 		&.actived {
			// 			background: #F27233;
			// 			border: 2rpx solid rgba(242, 114, 51, 1);
			// 			color: white;
			// 		}

			// 		.discount-text {
			// 			display: inline;
			// 			color: red;
			// 			// border: 1rpx solid red;
			// 			border-radius: 50%;
			// 			margin-left: 10rpx;
			// 			// position: absolute;
			// 			right: 10rpx;
			// 			top: -30rpx;
			// 		}
			// 	}
			// }

			.total-list-new {
				flex-grow: 1;
				display: flex;
				justify-content: space-between;
				position: relative;
				padding-top: 30rpx;

				.item {
					// flex-grow: 1;
					text-align: center;
					// display: flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;
					font-weight: 500;
					border-radius: 10rpx;
					border: 2rpx solid #e1e1e1;
					// background: #e1e1e1;
					padding: 20rpx 0rpx;
					flex: 0 0 30%;
					width: 30%;
					// margin: 0rpx 10rpx;
					// margin-left: 20rpx;
					font-size: 24rpx;
					position: relative;

					.tips {
						position: absolute;
						right: 0rpx;
						top: -20rpx;
						background: white;
						font-size: 20rpx;
						color: red;
						font-weight: normal;
						border-radius: 20rpx;
						border: 1px solid red;
						padding: 0rpx 10rpx;
					}

					.discount-text {
						display: inline;
						color: red;
						// border: 1rpx solid red;
						border-radius: 50%;
						margin-left: 10rpx;
						// position: absolute;
						right: 10rpx;
						top: -30rpx;

					}

					.total-text {
						font-size: 30rpx;
						font-weight: 500;
					}

					&.actived {
						background: #F27233;
						border: 2rpx solid rgba(242, 114, 51, 1);
						color: white;

						.discount-text {
							color: white;
						}
					}

				}
			}

			.price-c {}

			.redpack-switch {
				margin-left: 10rpx;
				position: relative;
				left: 10rpx;
				transform: scale(0.8);
			}
		}

		.gray-text {
			font-size: 28rpx;
			color: gray;
		}

		.total {
			margin-top: 20rpx;
			width: 100%;
			box-sizing: border-box;
			padding: 0 0rpx;
			text-align: right;
			color: red;
			font-size: 28rpx;
			margin-bottom: 50rpx;
		}



		.button {

			padding: 0;
			margin: 50rpx auto 0rpx auto;
			width: 100%;
			border-radius: 50rpx;
			height: 86rpx;
			line-height: 86rpx;
			text-align: center;
			font-size: 30rpx;
			font-weight: bold;
			background-color: #F27233;
			color: white;
			box-shadow: 0 0 20rpx rgba(242, 114, 51, 0.6);


			&:after {
				border: none;
			}

			&.disabled {
				background-color: #ddd;
				color: black;
				box-shadow: 0 0 20rpx #ddd;
			}
		}
	}
</style>
